<template>
  <div class="default-main ba-table-box">
    <!-- 表格顶部菜单 -->
    <TableHeader
      :buttons="[
        'refresh',
        'add',
        'edit',
        'comSearch',
        'columnDisplay',
        'unfold',
      ]"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 表格 -->
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" :pagination="false" @action="baTable.onTableAction" />
    <!-- 表单 -->
    <PopupForm v-if="baTable.form.operate ? true : false" />
    <!-- 问题反馈表单 -->
  </div>
</template>
<script setup lang="ts">
import {
  ref,
  provide,
  onMounted,
  markRaw,
  reactive,
  onBeforeUnmount,
} from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import { FactoryStation } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "./popupForm.vue";
// import TagVue from "./components/tag.vue";
import TagSearch from "@/components/tagSearch/index.vue";
import { useRoute, useRouter } from "vue-router";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useNavTabs } from "@/store/navTabs";
import { getDictData } from "@/utils/common";
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useFactoryStation } from "@/store/backend/FactoryM/stationInfo";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const router = useRouter();
const navTabs = useNavTabs();
const dictionary = useDictionary();
const stationInfo = useFactoryStation();
//全局事件总线
const { proxy } = useCurrentInstance();

const state: { feedback: boolean } = reactive({ feedback: false });

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + FactoryStation)
      ? (navTabs as any).pageSizeMap.get(route.path + FactoryStation)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(FactoryStation), //this.api = api部门管理
  {
    expandAll: true,
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("id"),
        label: "ID",
        prop: "id",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("code"),
        label: "工作中心代码",
        prop: "code",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("name"),
        label: "工作中心描述",
        prop: "name",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: operatorShowOff([
          route.path + "/edit",
          route.path + "/add",
          route.path + "/remove",
        ]),
        label: "操作",
        align: "center",
        operator: false,
        render: "buttons",
        fixed: "right",
        width: "220",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            //所有itemAdd都要在battable文件onTableButtonClick的方法中单独设置
            render: "itemAddButton",
            name: "itemAdd",
            title: "新增",
            text: "",
            type: "primary",
            icon: "iconfont icon-xinzeng",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageSize: pageSizeKeep(),
    },
    extend: {
      maxHeight: 600,
    },
  },
  {},
  {}
);

//获取计划状态/是否暂停的字典
const getMenuStatus = () => {
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["productType"])
  ) {
    dictionary.getDictInfo("sys_plan_producttype").then((res: any) => {
      dictionary.moreTags["productType"] = getDictData(res);
      console.log("获取生产类型的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["planState"])
  ) {
    dictionary.getDictInfo("sys_plan_state").then((res: any) => {
      dictionary.moreTags["planState"] = getDictData(res);
      console.log("获取计划状态的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["isSuspend"])
  ) {
    dictionary.getDictInfo("sys_plan_suspend").then((res: any) => {
      dictionary.moreTags["isSuspend"] = getDictData(res);
      console.log("获取是否暂停的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["priority"])
  ) {
    dictionary.getDictInfo("sys_plan_priority").then((res: any) => {
      dictionary.moreTags["priority"] = getDictData(res);
      console.log("获取优先级的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["isProduct"])
  ) {
    dictionary.getDictInfo("sys_plan_isproduct").then((res: any) => {
      dictionary.moreTags["isProduct"] = getDictData(res);
      console.log("获取是否排产的字典", dictionary.moreTags);
    });
  }
};
//   getMenuStatus();

// 双击查看工艺路线
// proxy.eventBus.on("toMonitor", (row: any) => {
//   console.log("双击查看工艺路线", row, row.id);
//   plan.$patch((state) => {
//     state.single = row;
//     state.planId = row.id;
//   });
// });

//跳转详情页
const infoButtonClick = (id: number, row: any) => {
  console.log("id", id, "row", row);
  stationInfo.$patch((state: any) => {
    state.single = row;
  });
  //   router.push({ path: "/admin/FactoryStation/detail" });
};

onMounted(() => {
  //全局事件总线
  const { proxy } = useCurrentInstance();
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(FactoryStation);
  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {}
  );
});

onBeforeUnmount(() => {
  proxy.eventBus.off("toMonitor");
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("dictionary", dictionary);
provide("stationInfo", stationInfo);
</script>
<style lang="scss" scoped>
.default-main {
  margin: 0px !important;
}
</style>
